//
//  testScrollViewViewControllerViewController.m
//  testScrollViewViewController
//
//  Created by imac  on 13-7-10.
//  Copyright (c) 2013年 imac . All rights reserved.
//

#import "testScrollViewViewControllerViewController.h"
#import "Define.pch"
#import "ViewController.h"
#import "goodsViewController.h"
@interface testScrollViewViewControllerViewController ()

@end

@implementation testScrollViewViewControllerViewController

@synthesize scrollView, slideImages;
@synthesize text;
@synthesize pageControl;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
    // 定时器 循环
//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
    // 初始化 scrollview
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH , SCREENHEIGHT)];
    
    //是否弹簧效果
    scrollView.bounces = YES;
    //是否翻页
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    //设置试图是否可以接受到用户的事件和消息
    scrollView.userInteractionEnabled = YES;
    //水平方向的滚动指示
    scrollView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:scrollView];
    
    // 初始化 数组 并添加四张图片
    slideImages = [[NSMutableArray alloc] init];
    [slideImages addObject:@"111.jpg"];
    [slideImages addObject:@"112.jpg"];
    [slideImages addObject:@"113.jpg"];
    [slideImages addObject:@"114.jpg"];
    
    
    // 初始化 pagecontrol
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREENWIDTH/3,SCREENHEIGHT-100,100,18)];
    
    // 初始化mypagecontrol
    [pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
    [pageControl setPageIndicatorTintColor:[UIColor blackColor]];
    
    pageControl.numberOfPages = [self.slideImages count];
    
    pageControl.currentPage = 0;
    
    [pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged]; // 触摸mypagecontrol触发change这个方法事件
    [self.view addSubview:pageControl];
    
    // 创建四个图片 imageview
    for (int i = 0;i<[slideImages count];i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
        imageView.frame = CGRectMake((SCREENWIDTH * i) + SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);
        [scrollView addSubview:imageView]; // 首页是第0页,默认从第1页开始的。所以+320。。。
    }
    // 取数组最后一张图片 放在第0页
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
    
    imageView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT); // 添加最后1页在首页 循环
    
    [scrollView addSubview:imageView];
    
    // 取数组第一张图片 放在最后1页
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]]; 
    imageView.frame = CGRectMake((SCREENWIDTH * ([slideImages count] + 1)) , 0, SCREENWIDTH, SCREENHEIGHT); // 添加第1页在最后 循环
    [scrollView addSubview:imageView];
    
    
    [scrollView setContentSize:CGSizeMake(SCREENWIDTH * ([slideImages count] + 2), SCREENHEIGHT)];
    
   
    [scrollView setContentOffset:CGPointMake(0, 0)];
    
    
    [self.scrollView scrollRectToVisible:CGRectMake(SCREENWIDTH,0,SCREENWIDTH,SCREENHEIGHT) animated:NO];
    
    button =[[UIButton alloc]initWithFrame:CGRectMake(SCREENWIDTH-100 ,SCREENHEIGHT-60,100, 60)];
    [button setTitle:@"登录页面"  forState:UIControlStateNormal];
    button.hidden=YES;
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(toMain) forControlEvents:UIControlEventTouchUpInside];
    // 默认从序号1位置放第1页 ，序号0位置位置放第4页
}
// scrollview 委托函数
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pagewidth = self.scrollView.frame.size.width;
    
    int page = floor((self.scrollView.contentOffset.x - pagewidth/([slideImages count]+2))/pagewidth)+1;
    
    page --;  // 默认从第二页开始
    NSLog(@"当前页：%d",page);
    pageControl.currentPage = page;
    if (pageControl.currentPage==3) {
        button.hidden=NO;
    }else{
        button.hidden=YES;
    }
}
// scrollview 委托函数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    CGFloat pagewidth = self.scrollView.frame.size.width;
    
    int currentPage = floor((self.scrollView.contentOffset.x - pagewidth/ ([slideImages count]+2)) / pagewidth) + 1;
    

    if (currentPage==0)
    {
//        [self toMain];
        //button.hidden=YES;
        [self.scrollView scrollRectToVisible:CGRectMake(SCREENWIDTH * [slideImages count],0,SCREENWIDTH,SCREENHEIGHT) animated:NO]; // 序号0 最后1页
    }
    else if (currentPage==([slideImages count]+1))
    {
        [self.scrollView scrollRectToVisible:CGRectMake(SCREENWIDTH,0,SCREENWIDTH,SCREENHEIGHT) animated:NO]; // 最后+1,循环第1页
        //button.hidden=YES;
    }else if (pageControl.currentPage==3) {
        //button.hidden=NO;
        
    }

}

-(void)toMain{
//    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    UIViewController *mainView=[story instantiateViewControllerWithIdentifier:@"viewController"];
//    [self presentViewController:mainView animated:YES completion:nil];

    goodsViewController *good=[[goodsViewController alloc]init];
    [self presentViewController:good animated:YES completion:nil ];
}
// pagecontrol 选择器的方法
- (void)turnPage
{
    int page = pageControl.currentPage; // 获取当前的page
    [self.scrollView scrollRectToVisible:CGRectMake(SCREENWIDTH*(page+1),0,SCREENWIDTH,SCREENHEIGHT) animated:NO]; // 触摸pagecontroller那个点点 往后翻一页 +1
    
}
// 定时器 绑定的方法
//- (void)runTimePage
//{
//    int page = pageControl.currentPage; // 获取当前的page
//    page++;
//    page = page > 3 ? 0 : page ;
//    pageControl.currentPage = page;
//    [self turnPage];
//}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
